package com.approval.approvalapp.index.cygn.hys;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.base.ContantActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.suke.widget.SwitchButton;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HysShenqingActivity extends BaseActivity {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.hyzt_tv)
    TextView hyztTv;
    @Bind(R.id.hyzt_ll)
    LinearLayout hyztLl;
    @Bind(R.id.kssj_tv)
    TextView kssjTv;
    @Bind(R.id.kssj_ll)
    LinearLayout kssjLl;
    @Bind(R.id.jssj_tv)
    TextView jssjTv;
    @Bind(R.id.jssj_ll)
    LinearLayout jssjLl;
    @Bind(R.id.khsx_tv)
    TextView khsxTv;
    @Bind(R.id.khsx_ll)
    LinearLayout khsxLl;
    @Bind(R.id.switch_button)
    SwitchButton switchButton;
    @Bind(R.id.chrs_tv)
    TextView chrsTv;
    @Bind(R.id.chrs_ll)
    LinearLayout chrsLl;
    @Bind(R.id.sqr_tv)
    TextView sqrTv;
    @Bind(R.id.sqr_ll)
    LinearLayout sqrLl;
    @Bind(R.id.lxdh_tv)
    TextView lxdhTv;
    @Bind(R.id.lxdh_ll)
    LinearLayout lxdhLl;
    public boolean sfxyty;
    @Bind(R.id.hynr_et)
    EditText hynrEt;
    @Bind(R.id.bz_et)
    EditText bzEt;
    @Bind(R.id.commit_tv)
    TextView commitTv;
    private String id;
    private String imPlaceN;

    @Override
    public int getLayoutId() {
        return R.layout.activity_hys_shenqing;
    }

    @Override
    public void initView() {
        id = getIntent().getStringExtra("id");
        imPlaceN = getIntent().getStringExtra("imPlaceN");
        titleTv1.setText("申请使用会议室" + imPlaceN);
        switchButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                if (isChecked) {
                    sfxyty = true;
                } else {
                    sfxyty = false;
                }
            }
        });

        sqrTv.setText(SpUtil.getString(this, SpUtil.USER_RUN_NICKNAME, ""));

    }

    @Override
    public void initData() {

    }

    String hyztstr;
    String khsxStr;
    String chrsStr;
    String sqrStr;
    String lxdhStr;

    private long kstime;
    private String kstimestr;
    private long jstime;
    private String jstimestr;

    @OnClick({R.id.back_iv, R.id.hyzt_ll, R.id.kssj_ll, R.id.jssj_ll,
            R.id.khsx_ll, R.id.chrs_ll, R.id.sqr_ll, R.id.lxdh_ll,
            R.id.commit_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.hyzt_ll:
                ContantActivity.goInto(this, 1, "会议主题", hyztstr);
                break;
            case R.id.kssj_ll:
                TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {

                        kstime = date.getTime() / 1000;
                        kstimestr = kstime + "";

                        SimpleDateFormat format = new SimpleDateFormat("yyyy MM-dd HH:mm:ss");
                        kssjTv.setText(format.format(date));


                        if (!TextUtils.isEmpty(jstimestr)) {
                            //如果已经选择了结束时间 需要判断 开始时间是否小于结束时间
                            if (kstime > jstime) {
                                kstimestr = "";
                                Toast.makeText(HysShenqingActivity.this, "开始时间不能大于结束时间", Toast.LENGTH_SHORT).show();
                                kssjTv.setText("选择时间");
                                return;
                            }
                        }


                    }
                }).setType(new boolean[]{true, true, true, true, true, false})
                        .setTitleText("选择开始时间").build();
                pvTime.show();
                break;
            case R.id.jssj_ll:
                TimePickerView pvTime1 = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {

                        jstime = date.getTime() / 1000;
                        jstimestr = jstime + "";


                        SimpleDateFormat format = new SimpleDateFormat("yyyy MM-dd HH:mm:ss");
                        jssjTv.setText(format.format(date));

                        if (!TextUtils.isEmpty(kstimestr)) {
                            //如果已经选择了开始时间 需要判断 结束时间是否小于开始时间
                            if (kstime > jstime) {
                                jstimestr = "";
                                jssjTv.setText("选择时间");
                                Toast.makeText(HysShenqingActivity.this, "结束时间不能小于结束时间", Toast.LENGTH_SHORT).show();
                                return;
                            }
                        }

                    }
                }).setType(new boolean[]{true, true, true, true, true, false})
                        .setTitleText("选择结束时间").build();
                pvTime1.show();
                break;
            case R.id.khsx_ll:
                ContantActivity.goInto(this, 2, "开会事项", khsxStr);
                break;
            case R.id.chrs_ll:
                ContantActivity.goInto(this, 3, "参会人数", chrsStr);
                break;
            case R.id.sqr_ll:
//                ContantActivity.goInto(this, 4, "申请人", sqrStr);
                break;
            case R.id.lxdh_ll:
                ContantActivity.goInto(this, 5, "联系电话", lxdhStr);
                break;
            case R.id.commit_tv:
                JSONObject jsonObject = new JSONObject();
                if (TextUtils.isEmpty(hyztstr) ||
                        TextUtils.isEmpty(kstimestr)
                        ||
                        TextUtils.isEmpty(jstimestr)
                        ||
                        TextUtils.isEmpty(lxdhStr)
                        ||
                        TextUtils.isEmpty(hynrEt.getText().toString())
                        ||
                        TextUtils.isEmpty(khsxStr)
                        ) {
                    Toast.makeText(this, "请输入完整内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                jsonObject.put("title", hyztstr);
                jsonObject.put("start", kstime + "");
                jsonObject.put("end", jstime + "");
                jsonObject.put("tel", lxdhStr);
                jsonObject.put("name", SpUtil.getString(this,
                        SpUtil.USER_RUN_NICKNAME, ""));
                jsonObject.put("content", hynrEt.getText().toString());
                if (!TextUtils.isEmpty(bzEt.getText().toString())) {
                    jsonObject.put("bz", bzEt.getText().toString());
                }

                if (sfxyty) {
                    jsonObject.put("is_ty", "1");
                } else {
                    jsonObject.put("is_ty", "0");
                }

                jsonObject.put("org_code", SpUtil.getString(this,
                        SpUtil.ORG_CODE, ""));

                jsonObject.put("org_name",
                        SpUtil.getString(this,
                                SpUtil.UNIT_NAME, ""));

                jsonObject.put("imPlaceN", imPlaceN);
                jsonObject.put("imPlace", id);
                jsonObject.put("fdsxmc", khsxStr);


                NetRequsetUtil.getInstance().netRequestPostJson("v1/center/my_meeting_function",
                      jsonObject.toString()  , new NetDownResponse() {
                            @Override
                            public void success(String str) {
                                Toast.makeText(HysShenqingActivity.this, "申请成功", Toast.LENGTH_SHORT).show();
                                HysShenqingActivity.this.finish();
                            }

                            @Override
                            public void errowithresponse(String str) {
                                ErrorBeanUtil.getErrorMessage(str);
                            }

                            @Override
                            public void erro() {

                            }

                            @Override
                            public void finish() {

                            }
                        });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (data != null) {
                    hyztstr = data.getStringExtra(ContantActivity.returnkey);
                    hyztTv.setText(hyztstr);
                }
                break;
            case 2:
                if (data != null) {
                    khsxStr = data.getStringExtra(ContantActivity.returnkey);
                    khsxTv.setText(khsxStr);
                }
                break;
            case 3:
                if (data != null) {
                    chrsStr = data.getStringExtra(ContantActivity.returnkey);
                    chrsTv.setText(chrsStr);
                }
                break;
            case 4:
                if (data != null) {
                    sqrStr = data.getStringExtra(ContantActivity.returnkey);
                    sqrTv.setText(sqrStr);
                }
                break;
            case 5:
                if (data != null) {
                    lxdhStr = data.getStringExtra(ContantActivity.returnkey);
                    lxdhTv.setText(lxdhStr);
                }
                break;
        }
    }

}
